package com.business.user.rabbitMq;

import com.alibaba.fastjson.JSONObject;
import com.business.common.enums.RabbitMqConstant;
import com.business.common.utils.StrKit;
import com.business.user.entity.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;


/**
 * 发送消息工具类
 *
 * @author chenjiehong
 * @date 2019-07-20
 */

@Component
public class RabbitMqSender {

    @Resource
    private RabbitTemplate rabbitTemplate;


    /**
     * 发送验证码
     */
    public void sendVerificationCode(String phone) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String routKey = RabbitMqConstant.SEND_VERIFICATION_CODE;
        JSONObject params = new JSONObject();
        params.put("phone", phone);
        rabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_DIRECT, routKey, params, correlationData);
    }

    /**
     * 发送短信
     */
    public void sendSms(String phone, String username, String dateTime, String templateCode) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String routKey = RabbitMqConstant.SMS;
        JSONObject params = new JSONObject();
        params.put("phone", phone);
        params.put("templateCode", templateCode);
        params.put("username", username);
        if (StrKit.isNotEmpty(dateTime)) {
            params.put("dateTime", dateTime);
        }
        rabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_DIRECT, routKey, params, correlationData);
    }


    /**
     * 发送用户数量添加
     */
    public void sendAddUserDataQuantity(Integer userId, String module, Integer quantity) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String routKey = RabbitMqConstant.SEND_USER_DATA_QUANTITY;
        JSONObject params = new JSONObject();
        params.put("userId", userId);
        params.put("module", module);
        params.put("quantity", quantity);
        rabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_DIRECT, routKey, params, correlationData);
    }

    /**
     * 添加消息
     */
    public void addMessage(Message message) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        String routKey = RabbitMqConstant.ADD_MESSAGE;
        rabbitTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_DIRECT, routKey, message, correlationData);
    }

}
